﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Menu : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public string Id { get; set; }
        public string MaxWidth { get; set; }
        public List<MenuSection> MenuSections { get; set; }

        public Menu()
        {
            MenuSection ms = new MenuSection();
            this.MenuSections = new List<MenuSection>();
            this.MenuSections.Add(ms);
        }

        public Menu(XmlNode node)
        {
            try
            {
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.MaxWidth = XmlHelper.GetAttribute(node, "MaxWidth");

                this.MenuSections = new List<MenuSection>();

                foreach (XmlNode childNode in node.SelectNodes("MenuSection"))
                {
                    MenuSection ms = new MenuSection(childNode);
                    this.MenuSections.Add(ms);
                }

                if (this.MenuSections.Count == 0)
                {
                    MenuSection ms = new MenuSection();
                    this.MenuSections.Add(ms);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode menuNode = XmlHelper.AddNode(commandUINode, "Menu");
                XmlHelper.AddAttribute(menuNode, "Id", this.Id);
                XmlHelper.AddAttribute(menuNode, "MaxWidth", this.MaxWidth);

                foreach (MenuSection ms in this.MenuSections)
                {
                    ms.AddNode(menuNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Menu");
                node.Tag = this;
                node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, new string[] { "MenuSection" }, true);

                foreach (MenuSection ms in this.MenuSections)
                {
                    ms.BuildTreeViewElement(node);
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is MenuSection))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (this.MenuSections == null)
                    {
                        this.MenuSections = new List<MenuSection>();
                    }

                    this.MenuSections.Add((MenuSection)child);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is MenuSection)
                {
                    this.MenuSections.Remove((MenuSection)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
